var JS_EMPLOYEE = function () {
    var owner = this;
    owner.dom_tag = "employee";
    owner.dom_edit = $("#" + owner.dom_tag + "_div_add_edit");
    owner.dom_table = $("#" + owner.dom_tag + "_table");
    owner.currentId = null;
    owner.fileUrl = null;
    owner.map_data = {};
    owner.sortName = "dateCreate";
    owner.sortOrder = "desc";
    owner.listBoxDep1 = new Array();
    owner.listBoxDep2 = new Array();
    owner.listBoxInterviewer = new Array();
    owner.listBoxHr = new Array();
    owner.mapBoxDeal = {"doing":"进行中", "done":"已完成"};

    owner.dom_table.datagrid({
        onSortColumn: function (sort, order) {
            owner.sortName = sort;
            owner.sortOrder = order;
            owner.getPage(1);
        }
    }).datagrid("getPager").pagination({
        layout: ["list", "sep", "first", "prev", "sep", "manual", "sep", "next", "last", "refresh", "sep"],
        pageList: [10, 20, 50, 100],
        pageSize: 20,
        rowStyler: function(index,row){
            if (row.dealStatus=="done"){
                return 'background-color:#6caef5;color:#fff;'; // return inline style
            }
        },
        buttons: [{
            iconCls: "icon-add",
            handler: function () {
                loadPage('添加简历', '/work/addEmployee');
            }
        }, {
            iconCls: "icon-edit",
            handler: function () {
                var row = owner.dom_table.datagrid("getSelected");
                loadPage('编辑简历', '/work/addEmployee?id='+row.itemId);
            }
        }],
        onSelectPage: function (pageNum, pageSize) {
            owner.getPage(pageNum, pageSize);
        }
    });

    $("#" + owner.dom_tag + "_search_dep1").combobox({
        onSelect: function (record) {
            owner.loadDep2(record.value, $("#" + owner.dom_tag + "_search_dep2"));
        }
    });

    $("#" + owner.dom_tag + "_ipt_inviteStatus").combobox({
        onSelect: function (record) {
            if (record.value=="已约面") {
                $("#" + owner.dom_tag + "_ipt_interviewTime").parent().show();
                $("#" + owner.dom_tag + "_ipt_interviewType").parent().show();
            } else {
                $("#" + owner.dom_tag + "_ipt_interviewTime").parent().hide();
                $("#" + owner.dom_tag + "_ipt_interviewType").parent().hide();
            }
        }
    });

    $("#" + owner.dom_tag + "_ipt_interviewStatus").combobox({
        onSelect: function (record) {
            if (record.value=="初始通过" || record.value=="复试通过") {
                $("#" + owner.dom_tag + "_ipt_interviewer").parent().show();
            } else {
                $("#" + owner.dom_tag + "_ipt_interviewer").parent().hide();
            }
        }
    });


    owner.init = function () {
        window.setTimeout(function () {
            $("#"+owner.dom_tag+"_search_entryDate0").Zebra_DatePicker({
                pair: $("#"+owner.dom_tag+"_search_entryDate1")
            });
            $("#"+owner.dom_tag+"_search_entryDate1").Zebra_DatePicker({
                direction: true
            });
        }, 1000);
        $("#" + owner.dom_tag + "_search_inviteStatus").combobox();
        $("#" + owner.dom_tag + "_search_interviewStatus").combobox();
        $("#" + owner.dom_tag + "_search_dealStatus").combobox();
        $("#" + owner.dom_tag + "_search_dep1").combobox();
        $("#" + owner.dom_tag + "_search_dep2").combobox();
        $("#" + owner.dom_tag + "_search_key").textbox();
        owner.loadDep1();
        owner.loadUserData();
        owner.getPage(1);
    };

    owner.loadDep1 = function () {
        $.ajax({
            type: "get",
            url: "/dep/getForList",
            cache: false,
            dataType: "json",
            data: {
                level: "1"
            }
        }).done(function (result) {
            if (result.success) {
                for (var key in result.data) {
                    owner.listBoxDep1.push({
                        value: key,
                        text: result.data[key]
                    });
                }
                $("#" + owner.dom_tag + "_search_dep1").combobox("loadData", owner.listBoxDep1);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.loadDep2 = function (father, dom) {
        $.ajax({
            type: "get",
            url: "/dep/getForList",
            cache: false,
            dataType: "json",
            data: {
                level: "2",
                father: father
            }
        }).done(function (result) {
            if (result.success) {
                owner.listBoxDep2 = new Array();
                for (var key in result.data) {
                    owner.listBoxDep2.push({
                        value: key,
                        text: result.data[key]
                    });
                }
                dom.combobox("loadData", owner.listBoxDep2);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.loadUserData = function () {
        $.ajax({
            type: "get",
            url: "/user/getForList",
            cache: false,
            dataType: "json",
            data: {}
        }).done(function (result) {
            if (result.success) {
                for (var i = 0; i < result.data.length; i++) {
                    var obj = result.data[i];
                    if (obj.type == "interviewer") {
                        owner.listBoxInterviewer.push({
                            value: obj.id,
                            text: obj.name
                        });
                    } else {
                        owner.listBoxHr.push({
                            value: obj.id,
                            text: obj.name
                        });
                    }
                }
                $("#" + owner.dom_tag + "_ipt_interviewer").combobox("loadData", owner.listBoxInterviewer);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.formatItem = function (obj, index) {
        var item = {
            "itemId": obj.id,
            "p0": index,
            "p1": obj.name,
            "p2": obj.jobName,
            "p3": obj.phone,
            "p4": obj.tips,
            "p5": obj.inviteStatus,
            "p6": obj.interviewStatus,
            "p7": owner.mapBoxDeal[obj.dealStatus],
            "dateEntry": parseFloat(obj.dateEntry).date(2),
            "dateCreate": obj.dateCreate.date(2),
            "p8": owner.generateOpHtml(obj.id)
        };
        return item;
    };

    owner.generateOpHtml = function (id) {
        return "<a class=\"easyui-menubutton\" onmouseover=\"jsEmployee.currentId='"+id+"'\" data-options=\"menu:'#"+owner.dom_tag+"_div_op',iconCls:'icon-config'\"></a>";
    };

    owner.getPage = function (pageNum, pageSize) {
        if (pageSize == null) {
            pageSize = owner.dom_table.datagrid("getPager").pagination("options").pageSize;
        }
        var params = {
            "inviteStatus": $("#" + owner.dom_tag + "_search_inviteStatus").combobox("getValue"),
            "interviewStatus": $("#" + owner.dom_tag + "_search_interviewStatus").combobox("getValue"),
            "dealStatus": $("#" + owner.dom_tag + "_search_dealStatus").combobox("getValue"),
            "dep1": $("#" + owner.dom_tag + "_search_dep1").combobox("getValue"),
            "dep2": $("#" + owner.dom_tag + "_search_dep2").combobox("getValue"),
            "entryDate0": $("#" + owner.dom_tag + "_search_entryDate0").val(),
            "entryDate1": $("#" + owner.dom_tag + "_search_entryDate1").val(),
            "key": $("#" + owner.dom_tag + "_search_key").textbox("getValue"),
            "pageNum": pageNum,
            "pageSize": pageSize,
            "sortName": owner.sortName,
            "sortOrder": owner.sortOrder
        };
        $.ajax({
            type: "get",
            url: "/employ/search",
            cache: false,
            dataType: "json",
            data: params
        }).done(function (result) {
            if (result.success) {
                var ps = result.data;
                var data = new Array();
                owner.map_data = {};
                if (ps.size == 0) {
                    $.messager.alert("提示信息", "暂无数据！");
                    return;
                }
                for (var i = 0; i < ps.list.length; i++) {
                    var obj = ps.list[i];
                    owner.map_data[obj.id] = obj;
                    data.push(owner.formatItem(obj, i+1));
                }
                owner.dom_table.datagrid("loadData", {
                    "total": ps.total,
                    "rows": data
                });
                $('a.easyui-menubutton').menubutton({
                    menuAlign: "right"
                });
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        });
    };

    owner.changeInviteStatus = function () {
        $("#" + owner.dom_tag + "_div_inviteStatus").window("open").window("center");
        $("#"+owner.dom_tag+"_ipt_interviewTime").Zebra_DatePicker();
    };

    owner.updateInviteStatus = function (dom) {
        dom.linkbutton("disable");
        $.ajax({
            type: "post",
            url: "/employ/updateInviteStatus",
            cache: false,
            dataType: "json",
            data: {
                "id": owner.currentId,
                "inviteStatus": $("#" + owner.dom_tag + "_ipt_inviteStatus").combobox("getValue"),
                "interviewTime": $("#" + owner.dom_tag + "_ipt_interviewTime").val(""),
                "interviewType": $("#" + owner.dom_tag + "_ipt_interviewType").combobox("getValue")
            }
        }).done(function (result) {
            if (result.success) {
                owner.map_data[owner.currentId].inviteStatus = $("#" + owner.dom_tag + "_ipt_inviteStatus").combobox("getValue");
                $("#" + owner.dom_tag + "_div_inviteStatus").window("close");
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        }).complete(function () {
            dom.linkbutton("enable");
        });
    };

    owner.changeInterviewStatus = function () {
        $("#" + owner.dom_tag + "_div_interviewStatus").window("open").window("center");
    };

    owner.updateInterviewStatus = function (dom) {
        dom.linkbutton("disable");
        $.ajax({
            type: "post",
            url: "/employ/updateInterviewStatus",
            cache: false,
            dataType: "json",
            data: {
                "id": owner.currentId,
                "interviewStatus": $("#" + owner.dom_tag + "_ipt_interviewStatus").combobox("getValue"),
                "interviewer": $("#" + owner.dom_tag + "_ipt_interviewer").combobox("getValue")
            }
        }).done(function (result) {
            if (result.success) {
                owner.map_data[owner.currentId].interviewStatus = $("#" + owner.dom_tag + "_ipt_interviewStatus").combobox("getValue");
                $("#" + owner.dom_tag + "_div_interviewStatus").window("close");
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        }).complete(function () {
            dom.linkbutton("enable");
        });
    };

    owner.showAddFollow = function () {
        $("#" + owner.dom_tag + "_div_addFollow").window("open").window("center");
        $("#"+owner.dom_tag+"_ipt_followTime").Zebra_DatePicker();
    };

    owner.addFollow = function (dom) {
        dom.linkbutton("disable");
        $.ajax({
            type: "post",
            url: "/follow/add",
            cache: false,
            dataType: "json",
            data: {
                "employeeId": owner.currentId,
                "content": $("#" + owner.dom_tag + "_ipt_follow").textbox("getValue"),
                "nextFollow": $("#" + owner.dom_tag + "_ipt_followTime").val()
            }
        }).done(function (result) {
            if (result.success) {
                $("#" + owner.dom_tag + "_div_addFollow").window("close");
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        }).complete(function () {
            dom.linkbutton("enable");
        });
    };

    owner.changeFile = function () {
        $("#" + owner.dom_tag + "_div_file").window("open").window("center");
    };

    owner.upload = function(jobId) {
        var dtd = $.Deferred();
        if ($("#" + owner.dom_tag + "_ipt_file").filebox("getValue")==null) {
            owner.fileUrl = "";
            dtd.resolve();
        }
        $.upload({
            url: "/employ/upload",
            domId: $("#" + owner.dom_tag + "_ipt_file").data("id"),
            dataType: 'html',
            params: {
                dir: jobId
            },
            onComplate: function (result) {
                if (result.success) {
                    owner.fileUrl = result.data;
                    dtd.resolve();
                } else {
                    $.messager.alert("提示信息", result.message);
                    dtd.reject();
                }
            },
            onError: function () {
                $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
                dtd.reject();
            }
        });
        return dtd.promise();
    };

    owner.updateFile = function (dom) {
        dom.linkbutton("disable");
        $.when(owner.upload(owner.map_data[owner.currentId].jobId)).done(function () {
            $.ajax({
                type: "post",
                url: "/employ/updateFile",
                cache: false,
                dataType: "json",
                data: {
                    "id": owner.currentId,
                    "fileUrl": owner.fileUrl
                }
            }).done(function (result) {
                if (result.success) {
                    $("#" + owner.dom_tag + "_div_file").window("close");
                } else {
                    $.messager.alert("提示信息", result.message);
                }
            }).fail(function () {
                $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
            });
        }).always(function () {
            dom.linkbutton("enable");
        });
    };
};